package com.ruoyi.business.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.bizsys.domain.ExtMonnifyOrder;
import com.ruoyi.bizsys.domain.ExtMonnifyReservedAccounts;
import com.ruoyi.bizsys.domain.ExtReqLog;
import com.ruoyi.bizsys.domain.RepaymentOrder;
import com.ruoyi.bizsys.service.IExtMonnifyOrderService;
import com.ruoyi.bizsys.service.IExtMonnifyReservedAccountsService;
import com.ruoyi.bizsys.service.IExtReqLogService;
import com.ruoyi.bizsys.service.IRepaymentOrderService;
import com.ruoyi.business.component.RepaymentMqSendService;
import com.ruoyi.business.service.IBRepaymentService;
import com.ruoyi.business.service.IMonnifyRepaymentService;
import com.ruoyi.business.service.MonnifyCallBackService;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author cyh
 * @date 2022/6/11 2:34 下午
 */
@Service
@AllArgsConstructor
public class MonnifyCallBackServiceImpl implements MonnifyCallBackService {

    IRepaymentOrderService iRepaymentOrderService;

    IExtReqLogService extReqLogService;

    IExtMonnifyReservedAccountsService monnifyReservedAccountsService;

    IMonnifyRepaymentService monnifyRepaymentService;

    RedisCache redisCache;

    IExtMonnifyOrderService iExtMonnifyOrderService;

    IBRepaymentService ibRepaymentService;

    RepaymentMqSendService repaymentMqService;

    /**
     * 拷贝自 CallBackController.webHookMonnify 的基础上修改，不改变原有逻辑
     * @param sourceData
     */
    @Override
    public void webHookMonnifyTransaction(JSONObject sourceData) {

        JSONObject eventData = sourceData.getJSONObject("eventData");
        JSONObject product = eventData.getJSONObject("product");
        String paymentReference = eventData.getString("paymentReference");
        String type = product.getString("type");

        String custNo = "";
        if ("WEB_SDK".equals(type)) {
            RepaymentOrder repaymentOrder = new RepaymentOrder();
            repaymentOrder.setRepaymentNo(paymentReference);
            List<RepaymentOrder> repaymentOrders = iRepaymentOrderService.selectRepaymentOrderList(repaymentOrder);
            custNo = repaymentOrders.get(0).getCustNo();
        }
        if ("RESERVED_ACCOUNT".equals(type)) {
            custNo = getCustNoByAccountReference(product.getString("reference"));
        } else {
            RepaymentOrder repaymentOrder = new RepaymentOrder();
            repaymentOrder.setRepaymentNo(paymentReference);
            List<RepaymentOrder> repaymentOrders = iRepaymentOrderService.selectRepaymentOrderList(repaymentOrder);
            custNo = repaymentOrders.get(0).getCustNo();
        }
        ExtReqLog extReqLog = new ExtReqLog();
        extReqLog.setReqUrl("/webhook/monnify");
        extReqLog.setReqParam(sourceData.toString());
        extReqLog.setCustNo(custNo);
        extReqLog.setApiName("webhook-monnify");
        extReqLog.setReqTime(DateUtils.dateTimeNow());
        extReqLogService.insertExtReqLog(extReqLog);

        eventData.put("eventType",sourceData.getString("eventType"));
        monnifyTransaction(eventData, custNo);
    }



    @Transactional
    @Override
    public void monnifyTransaction(JSONObject parseObject, String custNo) {

        ExtMonnifyOrder extMonnifyOrder = monnifyRepaymentService.checkWebHookStatus(parseObject, custNo);

        if (extMonnifyOrder == null) {
            return;
        }
        //判断时transferTous还是card
        String type = parseObject.getJSONObject("product").getString("type");

        if ("RESERVED_ACCOUNT".equals(type)) {
            JSONObject jsonObjectForMq = createJsonData(extMonnifyOrder,custNo);
            repaymentMqService.syncSendMqRepayMentMonnify(jsonObjectForMq,custNo);

//            monnifyRepaymentService.repaymentLock(extMonnifyOrder);
//            //删除redis
//            redisCache.deleteObject(TRANSFER_TO_US_THREAD + custNo);
        }
    }

    private JSONObject createJsonData(ExtMonnifyOrder extMonnifyOrder,String custNo) {
        JSONObject res = new JSONObject();
        res.put("extMonnifyOrder",extMonnifyOrder);
        res.put("custNo",custNo);
        return res;
    }


    @Override
    public String getCustNoByAccountReference(String accountReference) {
        String custNo;
        QueryWrapper<ExtMonnifyReservedAccounts> monnifyReservedAccountsQueryWrapper = new QueryWrapper<>();
        monnifyReservedAccountsQueryWrapper.eq("account_reference", accountReference).eq("state", UserConstants.BUSINESS_NORMAL).select("cust_no", "account_reference").last("limit 1");
        ExtMonnifyReservedAccounts monnifyReservedAccounts = monnifyReservedAccountsService.getOne(monnifyReservedAccountsQueryWrapper);
        custNo = monnifyReservedAccounts.getCustNo();
        return custNo;
    }

}
